package datastrucure.tree;

import java.util.LinkedList;

/**
 * @Author Fizz Pu
 * @Date 2021/4/13 下午2:29
 * @Version 1.0
 * 失之毫厘，缪之千里！
 */



public class TraverseTree {
    static class Node{
        private Node left;
        private Node right;
        private  int val;

        public Node(Node left, Node right, int val) {
            this.left = left;
            this.right = right;
            this.val = val;
        }
    }

    public static  void order(Node node){
        if(node == null){
            return;
        }
        LinkedList<Node> stack = new LinkedList<>();
        stack.add(node);
        while (!stack.isEmpty()){
            Node tmp = stack.removeFirst();
            System.out.print(tmp.val + ", ");

            if(tmp.right != null){
                stack.addFirst(tmp.right);
            }

            if(tmp.left != null){
                stack.addFirst(tmp.left);
            }
        }
    }

    public static void main(String[] args) {
        Node node1 = new Node(null, null, 1);
        Node node2 = new Node(null, null, 2);
        Node node3 = new Node(null, null, 3);
        Node node4 = new Node(null, null, 4);
        Node node5 = new Node(null, null, 5);
        Node node6 = new Node(null, null, 6);
        Node node7 = new Node(null, null, 7);
        Node node8 = new Node(null, null, 8);
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;
        node6.left = node8;
        order(node1);
    }
}
